/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.LabBusyTime;
import me.zhengjie.modules.biz.service.LabBusyTimeService;
import me.zhengjie.modules.biz.service.dto.LabBusyTimeQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-16
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "lab-busy-time管理")
@RequestMapping("/api/labBusyTime")
public class LabBusyTimeController {

    private final LabBusyTimeService labBusyTimeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/queryLabBusyTimeSelf/download")
//    @PreAuthorize("@el.check('labBusyTime:list')")
    public void exportLabBusyTime(HttpServletResponse response, LabBusyTimeQueryCriteria criteria) throws IOException {
        labBusyTimeService.download(labBusyTimeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询lab-busy-time")
    @ApiOperation("查询lab-busy-time")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabBusyTime(LabBusyTimeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(labBusyTimeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增lab-busy-time")
    @ApiOperation("新增lab-busy-time")
    @AnonymousAccess
    public ResponseEntity<Object> createLabBusyTime(@Validated @RequestBody LabBusyTime resources){
        return new ResponseEntity<>(labBusyTimeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改lab-busy-time")
    @ApiOperation("修改lab-busy-time")
    @AnonymousAccess
    public ResponseEntity<Object> updateLabBusyTime(@Validated @RequestBody LabBusyTime resources){
        labBusyTimeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除lab-busy-time")
    @ApiOperation("删除lab-busy-time")
    @AnonymousAccess
    public ResponseEntity<Object> deleteLabBusyTime(@RequestBody Integer[] ids) {
        labBusyTimeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/queryLabBookedTime")
    @Log("查询某个实验室已预约时间")
    @ApiOperation("查询某个实验室已预约时间")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabBookedTimeById(@RequestParam Integer labId){
        return new ResponseEntity<>(labBusyTimeService.queryLabBookedTimeById(labId),HttpStatus.OK);
    }

    @PostMapping("/cancel")
    @Log("实验室预约取消")
    @ApiOperation("实验室预约取消")
    @AnonymousAccess
    public ResponseEntity<Object> cancel(@RequestParam Integer id){
        labBusyTimeService.cancel(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/approve")
    @Log("实验室预约审核通过")
    @ApiOperation("实验室预约审核通过")
    @AnonymousAccess
    public ResponseEntity<Object> approve(@RequestParam Integer id){
        if (labBusyTimeService.approve(id) == true) {
            return new ResponseEntity<>(0,HttpStatus.OK);
        }
        return new ResponseEntity<>(1,HttpStatus.OK);
    }

    @PostMapping("/reject")
    @Log("实验室预约审核未通过")
    @ApiOperation("实验室预约审核未通过")
    @AnonymousAccess
    public ResponseEntity<Object> reject(@RequestParam Integer id, @RequestParam(defaultValue = "") String auditNote){
        labBusyTimeService.reject(id, auditNote);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/queryAllFor3d")
    @Log("查询lab-busy-time")
    @ApiOperation("查询lab-busy-time")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabBusyTimeAllFor3d(LabBusyTimeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(labBusyTimeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/queryLabBusyTimeSelf")
    @Log("查询自己审核的实验室预约信息")
    @ApiOperation("查询自己审核的实验室预约信息")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabBusyTimeSelf(LabBusyTimeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(labBusyTimeService.queryLabBusyTimeSelf(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/queryUserFor3d")
    @AnonymousAccess
    public ResponseEntity<Object> queryLabBusyTimeUserFor3d(LabBusyTimeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(labBusyTimeService.queryUserAll(criteria,pageable),HttpStatus.OK);
    }
}
